{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.3 \u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u540d\u5bfc\u5165\u5305\u4e2d\u5b50\u6a21\u5757\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c06\u4ee3\u7801\u7ec4\u7ec7\u6210\u5305,\u60f3\u7528import\u8bed\u53e5\u4ece\u53e6\u4e00\u4e2a\u5305\u540d\u6ca1\u6709\u786c\u7f16\u7801\u8fc7\u7684\u5305\u4e2d\u5bfc\u5165\u5b50\u6a21\u5757\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u5305\u7684\u76f8\u5bf9\u5bfc\u5165\uff0c\u4f7f\u4e00\u4e2a\u6a21\u5757\u5bfc\u5165\u540c\u4e00\u4e2a\u5305\u7684\u53e6\u4e00\u4e2a\u6a21\u5757\n\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u5728\u4f60\u7684\u6587\u4ef6\u7cfb\u7edf\u4e0a\u6709mypackage\u5305\uff0c\u7ec4\u7ec7\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "mypackage/\n    __init__.py\n    A/\n        __init__.py\n        spam.py\n        grok.py\n    B/\n        __init__.py\n        bar.py"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u6a21\u5757mypackage.A.spam\u8981\u5bfc\u5165\u540c\u76ee\u5f55\u4e0b\u7684\u6a21\u5757grok\uff0c\u5b83\u5e94\u8be5\u5305\u62ec\u7684import\u8bed\u53e5\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# mypackage/A/spam.py\nfrom . import grok"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u6a21\u5757mypackage.A.spam\u8981\u5bfc\u5165\u4e0d\u540c\u76ee\u5f55\u4e0b\u7684\u6a21\u5757B.bar\uff0c\u5b83\u5e94\u8be5\u4f7f\u7528\u7684import\u8bed\u53e5\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# mypackage/A/spam.py\nfrom ..B import bar"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e24\u4e2aimport\u8bed\u53e5\u90fd\u6ca1\u5305\u542b\u9876\u5c42\u5305\u540d\uff0c\u800c\u662f\u4f7f\u7528\u4e86spam.py\u7684\u76f8\u5bf9\u8def\u5f84\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5305\u5185\uff0c\u65e2\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6765\u5bfc\u5165\u3002\n\u4e3e\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# mypackage/A/spam.py\nfrom mypackage.A import grok # OK\nfrom . import grok # OK\nimport grok # Error (not found)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u50cfmypackage.A\u8fd9\u6837\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u540d\u7684\u4e0d\u5229\u4e4b\u5904\u662f\u8fd9\u5c06\u9876\u5c42\u5305\u540d\u786c\u7f16\u7801\u5230\u4f60\u7684\u6e90\u7801\u4e2d\u3002\u5982\u679c\u4f60\u60f3\u91cd\u65b0\u7ec4\u7ec7\u5b83\uff0c\u4f60\u7684\u4ee3\u7801\u5c06\u66f4\u8106\uff0c\u5f88\u96be\u5de5\u4f5c\u3002 \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u4f60\u6539\u53d8\u4e86\u5305\u540d\uff0c\u4f60\u5c31\u5fc5\u987b\u68c0\u67e5\u6240\u6709\u6587\u4ef6\u6765\u4fee\u6b63\u6e90\u7801\u3002 \u540c\u6837\uff0c\u786c\u7f16\u7801\u7684\u540d\u79f0\u4f1a\u4f7f\u79fb\u52a8\u4ee3\u7801\u53d8\u5f97\u56f0\u96be\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e5f\u8bb8\u6709\u4eba\u60f3\u5b89\u88c5\u4e24\u4e2a\u4e0d\u540c\u7248\u672c\u7684\u8f6f\u4ef6\u5305\uff0c\u53ea\u901a\u8fc7\u540d\u79f0\u533a\u5206\u5b83\u4eec\u3002 \u5982\u679c\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\uff0c\u90a3\u4e00\u5207\u90fdok\uff0c\u7136\u800c\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u540d\u5f88\u53ef\u80fd\u4f1a\u51fa\u95ee\u9898\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "import\u8bed\u53e5\u7684 . \u548c .. \u770b\u8d77\u6765\u5f88\u6ed1\u7a3d, \u4f46\u5b83\u6307\u5b9a\u76ee\u5f55\u540d.\u4e3a\u5f53\u524d\u76ee\u5f55\uff0c..B\u4e3a\u76ee\u5f55../B\u3002\u8fd9\u79cd\u8bed\u6cd5\u53ea\u9002\u7528\u4e8eimport\u3002\n\u4e3e\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from . import grok # OK\nimport .grok # ERROR"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u770b\u8d77\u6765\u50cf\u662f\u6d4f\u89c8\u6587\u4ef6\u7cfb\u7edf\uff0c\u4f46\u662f\u4e0d\u80fd\u5230\u5b9a\u4e49\u5305\u7684\u76ee\u5f55\u4e4b\u5916\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f7f\u7528\u70b9\u7684\u8fd9\u79cd\u6a21\u5f0f\u4ece\u4e0d\u662f\u5305\u7684\u76ee\u5f55\u4e2d\u5bfc\u5165\u5c06\u4f1a\u5f15\u53d1\u9519\u8bef\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u76f8\u5bf9\u5bfc\u5165\u53ea\u9002\u7528\u4e8e\u5728\u5408\u9002\u7684\u5305\u4e2d\u7684\u6a21\u5757\u3002\u5c24\u5176\u662f\u5728\u9876\u5c42\u7684\u811a\u672c\u7684\u7b80\u5355\u6a21\u5757\u4e2d\uff0c\u5b83\u4eec\u5c06\u4e0d\u8d77\u4f5c\u7528\u3002\u5982\u679c\u5305\u7684\u90e8\u5206\u88ab\u4f5c\u4e3a\u811a\u672c\u76f4\u63a5\u6267\u884c\uff0c\u90a3\u5b83\u4eec\u5c06\u4e0d\u8d77\u4f5c\u7528\n\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "% python3 mypackage/A/spam.py # Relative imports fail"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u4f60\u4f7f\u7528Python\u7684-m\u9009\u9879\u6765\u6267\u884c\u5148\u524d\u7684\u811a\u672c\uff0c\u76f8\u5bf9\u5bfc\u5165\u5c06\u4f1a\u6b63\u786e\u8fd0\u884c\u3002\n\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "% python3 -m mypackage.A.spam # Relative imports work"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u66f4\u591a\u7684\u5305\u7684\u76f8\u5bf9\u5bfc\u5165\u7684\u80cc\u666f\u77e5\u8bc6,\u8bf7\u770b PEP 328 ."
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}